package com.qifn.blog.Controller.Admin;

import com.github.pagehelper.PageInfo;
import com.qifn.blog.Model.Tag;
import com.qifn.blog.Service.TagService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
@RequestMapping("/admin")
public class AdminTagController {

    @Resource
    TagService tagService;

    @GetMapping("/tag")
    public ModelAndView tag(@RequestParam(defaultValue = "1") Integer pageNum){
        ModelAndView mv = new ModelAndView();
        PageInfo<Tag> tagPageInfo = tagService.queryAdminAllTag(pageNum,4);
        mv.addObject("tagPageInfo",tagPageInfo);
        return mv;
    }

    @DeleteMapping("/deleteTag")
    public ModelAndView deleteTag(Integer tagId){
        ModelAndView mv = new ModelAndView();
        boolean flag = tagService.deleteTagById(tagId);
        if(flag){
            mv.addObject("message","标签删除成功");
        }else {
            mv.addObject("message","标签删除失败");
        }
        mv.setViewName("redirect:/admin/tag");
        return mv;
    }

    @PostMapping("/insertTag")
    public ModelAndView insertTag(String tagName){
        ModelAndView mv = new ModelAndView();
        Tag tag = new Tag();
        tag.setName(tagName);
        boolean flag = tagService.insertTag(tag);
        if(flag){
            mv.addObject("message","标签添加成功");
        }else {
            mv.addObject("message","标签添加失败");
        }
        mv.setViewName("redirect:/admin/tag");
        return mv;
    }
}
